﻿using System.Linq;
using IterationLife.FootballTeaching.Models;
using IterationLife.FootballTeaching.Repositories;
using Util;
using Util.Datas.Ef;

namespace IterationLife.Datas.Repositories.FootballTeachings
{
    /// <summary>
    /// 课程分类仓储
    /// </summary>
    public class CourseCategoryRepository : RepositoryBase<CourseCategory>, ICourseCategoryRepository
    {
        /// <summary>
        /// 初始化课程分类仓储
        /// </summary>
        /// <param name="unitOfWork">工作单元</param>
        public CourseCategoryRepository(IApplicationUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }

        /// <summary>
        /// 获取排序号
        /// </summary>
        public int GetSortId()
        {
            return Find().Max(t => t.SortId).SafeValue() + 1;
        }

        /// <summary>
        /// 是否重复
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="name">名称</param>
        /// <param name="id">编号</param>

        public bool IsRepeat(string code, string name, string id)
        {
            var entity = Find().FirstOrDefault(t => t.Code == code || t.Name == name);
            if (entity == null)
                return false;
            var entityId = id.ToGuidOrNull();
            if (entityId.IsEmpty())
                return true;
            return entity.Id != entityId;
        }
    }
}
